package com.easytouch.vmhdl.presenter;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.AsyncTask;

import com.easytouch.core.utils.Log;
import com.easytouch.core.utils.SDCardTools;
import com.easytouch.core.utils.SystemTools;
import com.easytouch.vmhdl.AppApplication;
import com.easytouch.vmhdl.interfaces.IManagerUpdateView;

import java.io.File;

/**
 * Created by gulin on 2018/1/25.
 */

public class ManagerUpdatePresenter {

    private Log mLogger = new Log("ManagerUpdatePresenter.txt");

    private IManagerUpdateView mView;

    public ManagerUpdatePresenter(IManagerUpdateView pView)
    {
        mView = pView;
    }

    @SuppressLint("StaticFieldLeak")
    public void doUDiskUpdate()
    {
        new AsyncTask<Void, Void, Integer>()
        {
            @Override
            protected void onPreExecute()
            {
                super.onPreExecute();
            }

            @Override
            protected Integer doInBackground(Void... params) {
                String uDiskPath = SDCardTools.getSDCardPath();
                File dir = new File(uDiskPath);
                if(!dir.exists())
                {
                    return IManagerUpdateView.OPT_UDISK_NOT_EXIST;
                }
                String apkPath = SDCardTools.getSDCardPath() + "/importData";
                dir = new File(apkPath);
                if(!dir.exists())
                {
                    return IManagerUpdateView.OPT_NO_APK;
                }
                //遍历找到目录下的apk文件
                File[] currentFiles;
                currentFiles = dir.listFiles();
                if (currentFiles == null)
                {
                    return IManagerUpdateView.OPT_NO_APK;
                }
                for (File file : currentFiles) {
                    if (file.isDirectory()) {
                        continue;
                    } else {
                        if(file.getName().endsWith(".apk"))
                        {
                            //SystemTools.
                        }
                    }
                }
                return IManagerUpdateView.OPT_SUCCESS;
            }

            @Override
            protected void onPostExecute(Integer result)
            {
                mView.showUDiskUpdateResultMessage(result);
                super.onPostExecute(result);
            }
        }.execute();
    }

    public void doNetUpdate()
    {
        Intent intent = new Intent("com.easytouch.remoteupdater.update");
        AppApplication.context.sendBroadcast(intent);
    }
}
